const Api = require('./api.js');
const http = require('./http.js');
const Rest = require('./rest.js');

function navigateBack() {
    uni.navigateBack({
        delta: 1,
        fail: function (res) {
            uni.switchTab({
                url: '/pages/index/index'
            });
        }
    });
}

function getAD(that, showad, thisPage) {
    Rest.get(Api.JIANGQIE_SETTING_AD)
        .then((res) => {
            that.setData({
                setAD: res.data
            });
            showad();
        })
        .catch((err) => {});
}


function getConfigs(that,type) {
  
   var params = {
       url: '/config/getConfigs',
       method: 'GET',
       data: {
		   type:type
	   },
       callBack: function (res) {
		   if(res.data.length > 0){
			   that.setData({
			       configs: res.data,
			   });
		   }
       }
   };
   http.request(params);
}

const formatTime = (date) => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`;
};

const formatNumber = (n) => {
    n = n.toString();
    return n[1] ? n : `0${n}`;
};

function formatDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
}

module.exports = {
    navigateBack,
    getAD,
    formatTime,
    formatDate,
	getConfigs
};
